package com.xidian.zhiancloud.controller.dangerWorkTicket.workManager.WorkTicket;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.dangerWorkTicket.WorkTicketTemplate;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketTemplateQO;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.TemplateSourceService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 第二页，危险作业申请的前半部分，通过id查找作业票名称与JSON信息，并反馈给前端渲染，再进一步填充值。
 *
 */
@RequestMapping("/WorkTicketTemplate")
@RestController
public class WorkTicketTemplateController extends BaseController {

    @Autowired
    private WorkTicketTemplateService service;

    @Autowired
    private TemplateSourceService templateSourceService;

    /**
     * 初始化作业票模板，新公司新加九条作业票模板记录。每个公司只能初始化一次
     *
     * @return
     */
    @GetMapping("/init")
    public ResultDTO<Boolean> init() {
        Long companyId = getSessionUserCompanyId();
        Boolean res = service.templateInit(companyId);
        return ResultDTO.successOf(true, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加作业票模板
     *
     * @param type
     * @param name
     * @return 是否添加成功
     */
    @PostMapping("/addTicketTemplate")
    public ResultDTO<Boolean> addTicketTemplate(@RequestParam("type") Integer type, @RequestParam("name") String name) {
        WorkTicketTemplate workTicketTemplate = new WorkTicketTemplate();
        workTicketTemplate.setCompanyId(getSessionUserCompanyId());
        workTicketTemplate.setName(name);
        workTicketTemplate.setType(type);
        workTicketTemplate.setContent(templateSourceService.getById(type).getContent());
        workTicketTemplate.setStatus(1);
        return ResultDTO.successOf(service.save(workTicketTemplate), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据id修改作业票模板内容
     *
     * @param workTicketTemplate
     * @return 是否修改成功
     */
    @PostMapping("/patchById")
    public ResultDTO<Boolean> patchById(WorkTicketTemplate workTicketTemplate){
        if (service.getById(workTicketTemplate.getId()) == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicketTemplate.setCompanyId(null);
        Boolean res = service.updateById(workTicketTemplate);
        return ResultDTO.successOf(res,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 列出作业票模板
     *
     * @return 作业票模板对象列表
     */
    @GetMapping("/getList")
    public ResultDTO<IPage<WorkTicketTemplate>> getList(WorkTicketTemplateQO workTicketTemplateQO){
        if (workTicketTemplateQO.getCurrent() == null || workTicketTemplateQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicketTemplateQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(service.getList(workTicketTemplateQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 更改作业票模板状态 —— 是否启用。0表示停用，1表示启用。
     *
     * @param workTemplateId
     * @param enableWorkTemplate
     * @return 是否修改成功
     */
    @PostMapping("/enableTemplate")
    public ResultDTO<Boolean> EnableTemplateById(@RequestParam("workTemplateId")Long workTemplateId, @RequestParam("enableWorkTemplate")Integer enableWorkTemplate) {
        WorkTicketTemplate workTicketTemplate = new WorkTicketTemplate();
        workTicketTemplate.setId(workTemplateId);
        workTicketTemplate.setCompanyId(getSessionUserCompanyId());
        workTicketTemplate.setStatus(enableWorkTemplate);
        boolean res = service.updateById(workTicketTemplate);
        return ResultDTO.successOf(res, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 根据ids删除作业票模板
     *
     * @param ids
     * @return 是否删除成功
     */
    @PostMapping("/deleteTemplate")
    public ResultDTO<Boolean> DeleteTemplateByIds(Long[] ids) {
        service.removeByIds(Arrays.asList(ids));
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }
}

